<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 15/11/21
 * Time: 下午9:53
 */
class Comment extends MY_Agent_Controller
{
    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $module = (int)$this->input->get_post('module');
        $uName  = $this->input->get_post('uname');
        $time   = $this->input->get_post('create_time');
        $status = $this->input->get_post('status');


        $where = array();
        $uName && $where['uname'] = $uName;
        $module  && $where['module'] = $module;
        ($status || $status === '0') && $where['status'] = $status;

        if ($time)
        {
            $eTime = explode('-', $time);//echo $eTime[0].'<br>';p(date('Y-m-d H:i:s', strtotime($eTime[0])));
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }


        $this->load->model('Model_comment', 'comment');

        $totalNum = $this->comment->count($where);
        $logInfo = $this->comment->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Comment/index', $totalNum, $limit, http_build_query($where));
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $this->load->model('model_user', 'user');
        $userData = $this->user->getUser(100000, 0, 'uname');


        $data = array(
            'title'     => '评论列表',
            'log_info'  => $logInfo,
            'user_data' => $userData,
            'pageHtml'  => $pageHtml,
            'time'      => $time,
            'status'    => $status,
            'uname'     => $uName,
            'module'    => $module,
            'totalNum'  => $totalNum,
        );


        $this->load->view('agent/comment/index', $data);
    }


    public function reply()
    {
        $response = array('code' => '0', 'msg' => '回复成功');

        do {
            $cid = (int)$this->input->get_post('cid');
            $reply_content = $this->input->get_post('reply_content');

            if (!$cid || empty ($reply_content))
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_comment', 'comment');
            $item = $this->comment->getOne(array('cid' => $cid));

            if (empty ($item))
            {
                $response = error(900000);
                break;
            }



            if (!empty ($item['reply_content']) || !empty ($item['reply_time']))
            {
                $response = error(900003);
                break;
            }


            $res = $this->comment->save(array('reply_content' => $reply_content, 'reply_time' => nowTime()), $cid ? $cid : null);
            if (!$res)
            {
                $response = error(900002);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $cid = (int)$this->input->get_post('cid');
            $status = (int)$this->input->get_post('status');

            if (!$cid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_comment', 'comment');
            $item = $this->comment->getOne(array('cid' => $cid));

            if (empty ($item))
            {
                $response = error(900000);
                break;
            }


            $commentStatus = config_item('comment_status');
            if (!array_key_exists($status, $commentStatus))
                $status = COMMENT_STATUS_NORMAL;


            $res = $this->comment->save(array('status' => $status), $cid);
            if (!$res)
            {
                $response = error(900001);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}